<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta charset="UTF-8">
 <title>查询结果</title>
 <style type="text/css">
 h1{ color: red;}
 .sql{font-weight: bold;}
 .first{color:red;}
 </style>
 </head>
 <body>
 <h1>根据首字查成语</h1><%
 String first=request.getParameter("first");
 if(first==null || first.trim().length()!=1){
     out.println("请正确输入一个汉字~");
     return;
 }
 String sql="select * from chengyu where first=?"; %>
 <p class='sql'><%=sql%></p>
 <p>以【<span class='first'><%=first%></span>】字开头的成语有：</p>
 <%
 Class.forName("com.mysql.cj.jdbc.Driver");
 try(Connection cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/db?allowMultiQueries=true", "root", "12qwas");
     PreparedStatement ps=cn.prepareStatement(sql)){
     ps.setString(1, first);
     try(ResultSet rs=ps.executeQuery()){
         while(rs.next()){
             String cy=rs.getString("cy");
             out.println(cy+" ");
         }
     }
 }
 %>
 </body>
 </html>
   